package com.es.www.annotation.advice;

import java.util.Arrays;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Component;

/**
 * @Created by michangtao
 * @Date 2022/2/7 17:07
 * @Description
 */
@Aspect
@Component
public class LogAdvice{

    /**
     * 定义切点
     */
    @Pointcut("execution(* com.es.www.annotation.service.FinanceService.find(..)))")
    public void logPoint(){
    }

    /**
     * 前置通知应用切点
     */
    @Before(value = "logPoint()")
    public void beforePrint(JoinPoint joinPoint){
        System.out.println("advice before run......");
        System.out.println("被拦截的类：" + joinPoint.getTarget().getClass().getName());
        System.out.println("被增强方法的参数：" + Arrays.toString(joinPoint.getArgs()));
        System.out.println("" + ((MethodSignature)joinPoint.getSignature()).getMethod().getParameterTypes());
        System.out.println("被增强方法的签名：" + joinPoint.getSignature());
        System.out.println("被增强方法：" + ((MethodSignature)joinPoint.getSignature()).getMethod().getName());
    }

    /**
     * 返回通知
     * 指定returning参数，可以接收方法返回参数
     */
    @AfterReturning(value = "execution(* com.es.www.annotation.service.FinanceService.find(..))", returning = "returnVal")
    public void afterReturningPrint(Object returnVal){
        System.out.println("advice afterReturning run.....");
        System.out.println("返回的数据：" + returnVal);
    }




}
